<?php
$printed=false;
$js=1;
if(isset($_GET['submit-course-search']) && $_GET['submit-course-search'] == 'Search') {
	unset($_GET['submit-course-search']);
	$js = 0;
}
if(isset($_GET['reset-search'])) {
	unset($_GET);
	if(isset($cacheparams)) { unset($cacheparams); }
	if(isset($formlocs)) { unset($formlocs); }
	if(isset($formterms)) { unset($formterms); }
}

if(!isset($cacheparams) && isset($_GET)) { $cacheparams = $_GET; }
$hashname = 'default-list';
/*var_dump($cacheparams);
die();*/
if(isset($cacheparams['hash'])) {
	$hashname = $cacheparams['hash'];
	unset($cacheparams['hash']);
}
elseif(isset($cacheparams) && !empty($cacheparams)) {
	$qs = array();
	foreach($cacheparams as $k=>$v) {
		if($k != 'pageinfo' && $k != 'id') {
			$qs[$k] = (is_array($_GET[$k])) ? $k.'[]='.implode('&'.$k.'[]=',$_GET[$k]) : $k.'='.$v;
		}
	}
	$qs = '?'.implode('&',$qs);
	$hashname = md5($qs);
}
if(array_key_exists('pageinfo',$cacheparams)) { unset($cacheparams['pageinfo']); }

if(!isset($paths) || !is_array($paths)) {
	if(isset($GLOBALS['paths']) && is_array($GLOBALS['paths'])) {
		$paths = $GLOBALS['paths'];
	}
	else {
		if(file_exists(realpath('./').'/paths.php')) {
			require_once(realpath('./').'/paths.php');
		}
		elseif(file_exists(realpath('../').'/paths.php')) {
			require_once(realpath('../').'/paths.php');
		}
	}
}
if(!class_exists('sis_course')) {
	require_once($paths['includepath'].'current-students/class-schedule-sis/sis.classes.php');
}

function prepArrayForFile($a=array()) {
	$a = array_filter($a);
	if(array_key_exists('howmany',$a) && $a['howmany'] == DEFHM) {
		unset($a['howmany']);
	}
	return (empty($a)) ? NULL : json_encode(array_filter($a));
	$rt = "array(\n";
	$ar = array();
	foreach($a as $k=>$v) {
		if(is_array($v)) {
			$k = ((is_numeric($k)) ? $k : "'".$k."'");
			$ar[] = $k.'=>'.prepArrayForFile($v);
		}
		else {
			$v = (is_numeric($v)) ? $v : "'$v'";
			$ar[] = "'$k'=>".$v;
		}
	}
	$rt .= implode(",\n",$ar).")";
	return $rt;
}

require_once($paths['htmlpath'].'sis-courses/course-list-variable-settings.php');

$cachedir = $paths['htmlpath'].'sis-courses/cache/';
$paramdir = $paths['htmlpath'].'sis-courses/params/';

/* First, we check to make sure it's after 3:30 this morning. If it's between midnight and 3:29 a.m., we won't do anything */
if(time() > strtotime(date('Y-m-d 03:30:00'),time())) {
	require_once($GLOBALS['paths']['includepath'].'current-students/class-schedule-sis/copy-SIS-DB.php');
	/* Next, we check to see if the cache directory exists. If it doesn't, we won't do anything here, as we are checking later to see if it exists and creating it if it doesn't. */
	if(is_dir(substr($cachedir,0,-1))) {
		/* If the directory does exist, we will check to see if the existing cache files were created before 3:30 this morning (for instance, earlier this morning or more likely yesterday). If they weren't, we won't do anything. */
		if(filemtime(substr($cachedir,0,-1)) < strtotime(date('Y-m-d 03:30:00'),time())) {
			/* If the cache files are older than 3:30 this morning, we will completely remove them. */
			shell_exec('rm -Rf '.substr($cachedir,0,-1));
		}
	}
}

/* Make sure that the cache directory and the paramater cache directory both exist */
foreach(array($cachedir,$paramdir) as $d) {
	$d = substr($d,0,-1);
	if(!is_dir($d)) {
		mkdir($d);
		chgrp($d,502);
		chmod($d,02775);
	}
}

/* Make sure the pageinfo GET parameter doesn't get included in our cache parameters */
if(isset($_GET['pageinfo'])) { unset($_GET['pageinfo']); }

/* Sort our cache parameters by key */
if(isset($cacheparams) && !empty($cacheparams)) { ksort($cacheparams); }
$filename = $cachedir.$hashname.'.cache';
/*print('<div style="debug">The file name is currently set to: '.$filename."\n<br /><pre><code>");
print_r($cacheparams);
print('</code></pre></div>');*/
/* Check to see if we have a cache file for this data */
if(!file_exists($filename)) {
	/* If we don't have a cache file for this data, we'll check to see if we have a cache file for these parameters */
	if(!file_exists(str_replace($cachedir,$paramdir,$filename))) {
		/* If not, we'll create one, as long as our parameters aren't empty */
		if(!empty($cacheparams)) {
			$p = prepArrayForFile($cacheparams);
			if(!is_null($p)) {
				file_put_contents(str_replace($cachedir,$paramdir,$filename),prepArrayForFile($cacheparams));
			}
		}
	}
	/* If the parameter cache file exists, we'll pull our parameters out of that file and store them in our GET global variable. */
	if(file_exists(str_replace($cachedir,$paramdir,$filename))) {
		$cacheparams = json_decode(file_get_contents(str_replace($cachedir,$paramdir,$filename)),true);
	}
	$_GET = $cacheparams;
	unset($_GET['hash']);

	/* Now that we've pulled our parameters out of our cache file, we'll set our start and howmany params appropriately. */
	if(isset($_GET['start']) && is_numeric($_GET['start'])) {
		$start = $_GET['start'];
		unset($_GET['start']);
	}
	else {
		$start = 0;
	}
	if(isset($_GET['howmany']) && is_numeric($_GET['howmany'])) {
		$howmany = $_GET['howmany'];
		if($_GET['howmany'] == DEFHM) {
			unset($_GET['howmany']);
		}
	}
	else {
		$howmany = DEFHM;
	}
	
	/* If our parameters are still empty, we'll change the hashname to default-list */
	$testparam = array_filter($_GET);
	if(empty($testparam) && $start == 0 && $howmany == DEFHM) {
		$filename = str_replace($hashname,'default-list',$filename);
		$hashname = 'default-list';
	}
	
	/* We'll check again to see if we have a cache file. If we don't, we'll create it and output the contents onto the screen */
	if(!file_exists($filename)) {
		ob_start();
		require_once($paths['htmlpath'].'sis-courses/lookup-query-info.php');
		$fcontents = ob_get_contents();
		$fcontents = str_replace('{{siteurl}}',$GLOBALS['paths']['siteurl'],$fcontents);
		file_put_contents($filename,$fcontents);
		chgrp($filename,502);
		chmod($filename,0664);
		ob_end_clean();
	}
	/* If we do, we'll just output the contents of the file onto the screen. */
	else {
		echo file_get_contents($filename);
		$printed=true;
	}
}
if(!$printed) {
	if(file_exists($filename)) {
		echo file_get_contents($filename);
	}
	else {
		require_once($paths['htmlpath'].'sis-courses/lookup-query-info.php');
	}
}
?>